<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login</title>
  <link rel="stylesheet" href="/static/css/concise-ui.min.css">
  <link rel="stylesheet" href="/static/css/concise.min.css">
  <link rel="stylesheet" href="/static/css/mgrid.css">
</head>
<body>
  <div page-box row>
    <div sm-12 md-2 lg-3></div>
    <div sm-12 md-8 lg-6>
      <form onsubmit="return false;">
        <input type="text" name="username"><br>
        <input type="password" name="passwd"><br>
        <input type="submit" value="登录" onclick="login()">
      </form>
    </div>
    <div sm-12 md-2 lg-3></div>
  </div>
  <script>
    function login () {
      let u = document.querySelector('[name=username]')
      let p = document.querySelector('[name=passwd]')
      let data = {
        username: u.value,
        passwd: p.value
      }

      fetch('/auth/adminlogin', {
        method: 'POST',
        body: JSON.stringify(data),
        headers: {
          'content-type': 'application/json'
        }
      }).then(res => {
        return res.json()
      }).then(d => {
        console.log(d)
        localStorage.setItem('token', d.token)
        localStorage.setItem('userinfo', JSON.stringify({
          name: d.username,
          role: d.role,
          level: d.level
        }))
        localStorage.setItem('token_time', Date.now())
      })

    }
  </script>
</body>
</html>